// pages/addGood/goodsCate/goodsCate.js
import {
  systematicsList,
  getProductBrand,
  brandLike,
  seriesLike
} from '../../../api.js'
let app = getApp();
Page({
  data: {
    lis_type: 1,
    lis: [],
    lis_text: {
      lis_one: {
        value: '',
        id: ''
      },
      lis_two: {
        value: '',
        id: ''
      },
    },
    btn_right: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var jsonStr = options.lis
    jsonStr = jsonStr.replace(/\ufeff/g, "");
    var jj = JSON.parse(jsonStr);
    console.log(jj)   
    var that = this
    wx.showLoading({
      title: '正在加载...',
    })
    console.log(jj.cag_title)
    if (jj.cag_title != ''){
      that.setData({
        btn_right:false
      })
    }
    that.data.lis_text.lis_one.value = jj.cag_title
    that.data.lis_text.lis_one.id = jj.category_id
    that.data.lis_text.lis_two.value = jj.type_title
    that.data.lis_text.lis_two.id = jj.type_id
    that.setData({
      lis_text: that.data.lis_text
    })
    that.up_lis()
  },
  lis_btn: function (e) {
    wx.showLoading({
      title: '正在加载...',
    })
    var id = e.currentTarget.dataset.id
    var ind = e.currentTarget.dataset.ind
    var that = this
    if (that.data.lis_type == 1) {
      that.data.lis_text.lis_one.value = that.data.lis[ind].title
      that.data.lis_text.lis_one.id = that.data.lis[ind].id
      that.setData({
        lis_text: that.data.lis_text,
        lis_type: 2
      })
      that.up_lis(id, 2)
      return
    }
    if (that.data.lis_type == 2) {
      that.data.lis_text.lis_two.value = that.data.lis[ind].title
      that.data.lis_text.lis_two.id = that.data.lis[ind].id
      that.setData({
        lis_text: that.data.lis_text,
        btn_right: false
      })
      wx.hideLoading()
      return
    }
  },
  up_lis(lis_id, lis_type) {
      systematicsList({
        token: getApp().globalData.token,
        parent_id: lis_id,
        // type: lis_type
      }).then(res => {
        if (res.data.code == 666) {
          this.setData({
            lis: res.data.data
          })
          if (res.data.data.length == 0) {
            this.setData({
              btn_right: false
            })
            wx.hideLoading()
          }
          wx.hideLoading()
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
  },

  btn_left(e) {
    wx.showLoading({
      title: '正在加载...',
    })
    this.setData({
      lis: []
    })
    var id = e.currentTarget.dataset.id
    var typ = e.currentTarget.dataset.typ
    var that = this
    this.up_lis(id, typ)
    if (typ == 0) {
      that.data.lis_text.lis_one.value = '',
        that.data.lis_text.lis_two.value = '',
        that.setData({
          btn_right: true,
          lis_text: that.data.lis_text,
          lis_type: 1
        })
    }
    if (typ == 2) {
      that.data.lis_text.lis_two.value = '',
        that.setData({
          btn_right: true,
          lis_text: that.data.lis_text,
          lis_type: 2
        })
    }
  },
  queren_btn(e) {
    console.log(this.data.lis_text)
    var lis_text = this.data.lis_text
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    
    prevPage.setData({
      'addEdit.cag_title': lis_text.lis_one.value,
      'addEdit.type_title': lis_text.lis_two.value,
      'addEdit.category_id': lis_text.lis_one.id,
      'addEdit.type_id': lis_text.lis_two.id,
    })
    if (prevPage.data.fuc == 0) {
      prevPage.productSn()
    }
    wx.navigateBack({
      delta: 1
    })
  }
})